<?php
class ReviewModel {
	var $DB;
	function __construct ($DB) {
		$this->DB = $DB;
	} 

	function getOne($id) {
		$sql = "SELECT * FROM movie_review WHERE id = '$id' ";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	}

	function getList($condition) {
		$sql = "SELECT * FROM movie_review ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} ";
		} 
		if (!empty($condition['page']) && !empty($condition['rows'])) {
			$start = ($condition['page'] - 1) * $condition['rows'];
			$tmp .= "LIMIT " . $start . ", " . $condition['rows'];
		} 
		$sql .= $tmp;
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}

	function total($condition) {
		$sql = "SELECT count(*) AS total FROM movie_review ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} ";
		} 
		$sql .= $tmp; 
		$this->DB->query($sql);
		$this->DB->fetchRow();
		return $this->DB->getValue("total");
	}

	function editOne($id,$title,$addtime,$contents) {
		$sql = "UPDATE movie_review SET title='$title',addtime='$addtime',contents='$contents' WHERE id = '$id'";
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}

	function delOne($id) {
		$sql = "DELETE FROM movie_review WHERE id = '$id'" ;
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	} 
} 

?>
